/*
 *  mini_db.h
 *
 *  Used for storing matrices of data in a 'name'/'value' pair.
 *  These mini databases may be saved or loaded from a file.
 *  Data may be added, modified, retrieved, and removed.
 *
 * TODO: add functions for reading/writing HTTP style "headers: value" over
 *       the wire.
 *
 * Copyright (c) 2006 Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *         This product includes software developed by
           Karlan Thomas Mitchell<karlanmitchell@gmail.com>
 * 4. Neither Karlan Thomas Mitchell nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY KARLAN THOMAS MITCHELL AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef ___MINI_DB_H
#define ___MINI_DB_H

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "encode.h"
#include "file.h"
#include "string.h"

#ifdef _LARGEFILE_SOURCE
	#ifndef atooff_t
	#define atooff_t atoll
	#endif
#else
	#ifndef atooff_t
	#define atooff_t atol
	#endif
#endif

struct st_mdb {
	char ** name;
	char ** value;
	size_t  items;
	size_t  allocated;
};

/*The ultimate debug function*/
int mdb_printall(struct st_mdb * db_data);

int mdb_save_tofd(struct st_mdb * db_data, int mdb_file);
int mdb_save(struct st_mdb * db_data, char * filename);
int mdb_load_fromfd(struct st_mdb * db_data, int mdb_file);
int mdb_load(struct st_mdb * db_data, char * filename);
int mdb_copy(struct st_mdb * new_db, struct st_mdb * old_db);
int mdb_init(struct st_mdb * db_data);
int mdb_free(struct st_mdb * db_data);
int mdb_reset(struct st_mdb * db_data);
int mdb_add_escaped_value(struct st_mdb * db_data, char * name, char * value);
int mdb_add_value(struct st_mdb * db_data, char * name, char * value);
int mdb_set_value(struct st_mdb * db_data, size_t loc, char *value);
size_t mdb_find(struct st_mdb * db_data, char * name);
size_t mdb_find_escaped(struct st_mdb * db_data, char * name);
int mdb_set(struct st_mdb * db_data, char * name, char * value);
int mdb_set_base32(struct st_mdb * db_data, char * name, char * value, size_t value_len);
int mdb_cat(struct st_mdb * db_data, char * name, char * value);
int    mdb_set_double(struct st_mdb * db_data, char * name, double value);
int    mdb_set_int(struct st_mdb * db_data, char * name, int value);
int    mdb_set_off_t(struct st_mdb * db_data, char * name, off_t value);
int    mdb_set_address(struct st_mdb * db_data, char * name, void * value);
char * mdb_print(struct st_mdb * db_data, char * name);
char * mdb_get(struct st_mdb * db_data, char * name);
void *  mdb_get_address(struct st_mdb * db_data, char * name);
double mdb_get_double(struct st_mdb * db_data, char * name);
int mdb_get_int(struct st_mdb * db_data, char * name);
off_t mdb_get_off_t(struct st_mdb * db_data, char * name);
char * mdb_get_name_by_number(struct st_mdb * db_data, int number);
char * mdb_get_value_by_number(struct st_mdb * db_data, int number);
size_t mdb_count_items(struct st_mdb * db_data);
int mdb_remove(struct st_mdb * db_data, char * name);

#endif
